﻿Type.registerNamespace("VisualCron");

VisualCron.VisualCronData = function () {

    VisualCron.VisualCronData.initializeBase(this);

    // fields
    this._serversList = null;
    this._jobs = [];
    this._serverGridClientID = null;
    this._serverFilter = '';
    this._serverTreeClientID = null;
}

VisualCron.VisualCronData.prototype = {

    // properties
    get_serversList: function () {
        return this._serversList;
    },

    set_serversList: function (val) {
        this._serversList = val;
        this._initServers(val);
    },

    get_serverGridClientID: function () {
        return this._serverGridClientID;
    },

    set_serverGridClientID: function (val) {
        this._serverGridClientID = val;
    },

    get_serverTreeClientID: function () {
        return this._serverTreeClientID;
    },

    set_serverTreeClientID: function (val) {
        this._serverTreeClientID = val;
    },

    //methods
    addServer: function (server) {
        var recordId = this._serversList.findSingle(function (toTest) { return toTest.ServerId == server.ServerId; });

        if (recordId == -1) {
            // we only add unique server connections
            this._serversList.push(server);
        }

        this.rebindServersGrid();

        if (!this._showHideServerNode(server.ServerId, true)) {
            this._addServerNode(server.Server, server.ServerId);
        }

        this.expandServer(server.ServerId);
    },

    addJobs: function (serverId, jobs, gridControlId) {
        var recordId = this._jobs.findSingle(function (toTest) { return toTest.get_serverId() == serverId; });

        var targetJobs = null;

        if (recordId == -1) {
            targetJobs = new VisualCron.JobsObject();
            targetJobs.set_serverId(serverId);
            this._jobs.push(targetJobs);
        }
        else {
            targetJobs = this._jobs[recordId];
        }

        if (gridControlId) {
            targetJobs.set_gridControlId(gridControlId);
        }

        targetJobs.set_jobArray(jobs);

    },

    addTasks: function (serverId, jobId, tasks, gridControlId, forceRebind) {
        var recordId = this._jobs.findSingle(function (toTest) { return toTest.get_serverId() == serverId; });

        if (recordId != -1) {
            targetJobs = this._jobs[recordId];

            targetJobs.addTasks(jobId, tasks, gridControlId, forceRebind);
        }
    },

    addSingleJob: function (serverId, job) {
        var recordId = this._jobs.findSingle(function (toTest) { return toTest.get_serverId() == serverId; });

        if (recordId == -1) {
            return;
        }

        this._jobs[recordId].addJob(job);
    },

    addSingleTask: function (serverId, jobId, task) {
        var recordId = this._jobs.findSingle(function (toTest) { return toTest.get_serverId() == serverId; });

        if (recordId != -1) {
            targetJobs = this._jobs[recordId];

            targetJobs.addSingleTask(jobId, task);
        }
    },

    expandServer: function (serverId) {
        // code for RadGrid
        if (this._serverGridClientID) {
            var serversGrid = $find(this._serverGridClientID).get_masterTableView();

            if (serversGrid) {
                var dataItems = serversGrid.get_dataItems();

                var rowAdjustment = 0;

                for (i = 0; i < dataItems.length; i++) {
                    if (dataItems[i].getDataKeyValue("ServerId") == serverId) {
                        if (dataItems[i].get_nestedViews() != null) {
                            serversGrid.expandItem(i + rowAdjustment);
                        }
                        return;
                    }
                    if (dataItems[i].get_nestedViews() != null) {
                        // rowAdjustment++;
                    }
                }
            }
        }

        //code for RadTreeView
        treeView = $find(this.get_serverTreeClientID());

        if (treeView) {
            var node = treeView.findNodeByValue(serverId);
            if (node) {
                node.set_expanded(true);
            }
        }
    },

    collapseServer: function (serverId) {
        if (this._serverGridClientID) {
            var serversGrid = $find(this._serverGridClientID).get_masterTableView();

            if (serversGrid) {
                var dataItems = serversGrid.get_dataItems();

                var rowAdjustment = 0;

                for (i = 0; i < dataItems.length; i++) {
                    if (dataItems[i].getDataKeyValue("ServerId") == serverId) {
                        if (dataItems[i].get_nestedViews() != null) {
                            serversGrid.collapseItem(i + rowAdjustment);
                        }
                        return;
                    }
                    if (dataItems[i].get_nestedViews() != null) {
                        // rowAdjustment++;
                    }

                }
            }
        }
    },

    removeServer: function (serverId) {

        this.collapseServer(serverId);

        var recordId = this.get_serversList().findSingle(function (toTest) { return toTest.ServerId == serverId; });

        if (recordId != -1) {
            this.get_serversList().splice(recordId, 1);
        }

        this.rebindServersGrid();

        this._showHideServerNode(serverId, false);

        //this._removeServerNode(serverId);
    },

    removeJob: function (serverId, job) {
        var recordId = this._jobs.findSingle(function (toTest) { return toTest.get_serverId() == serverId; });

        if (recordId == -1) {
            return;
        }

        this._jobs[recordId].removeJob(job);
    },

    removeTask: function (serverId, jobId, task) {
        var recordId = this._jobs.findSingle(function (toTest) { return toTest.get_serverId() == serverId; });

        if (recordId != -1) {
            targetJobs = this._jobs[recordId];

            targetJobs.removeTask(jobId, task);
        }
    },

    updateJob: function (serverId, job) {
        var recordId = this._jobs.findSingle(function (toTest) { return toTest.get_serverId() == serverId; });

        if (recordId == -1) {
            return;
        }

        this._jobs[recordId].updateJob(job);
    },

    updateTask: function (serverId, jobId, task) {
        var recordId = this._jobs.findSingle(function (toTest) { return toTest.get_serverId() == serverId; });

        if (recordId != -1) {
            targetJobs = this._jobs[recordId];

            targetJobs.updateTask(jobId, task);
        }
    },

    getServerByJobGrid: function (gridId) {
        var recordId = this._jobs.findSingle(function (toTest) { return toTest.get_gridControlId() == gridId; });

        if (recordId == -1) {
            return;
        }

        return this._jobs[recordId].get_serverId();
    },

    getServerAndJobByGrid: function (gridId) {
        var result = new Object();
        for (var i = 0; i < this._jobs.length; i++) {
            var jobId = this._jobs[i].getJobIdByTaskGridId(gridId);
            if (jobId) {
                result.serverId = this._jobs[i].get_serverId();
                result.jobId = jobId;
                return result;
            }
        }

        return null;
    },

    filterJobs: function (filter) {
        for (var i = 0; i < this._jobs.length; i++) {
            this._jobs[i].set_jobFilter(filter);
        }
    },

    filterServers: function (filter) {

        this._serverFilter = filter;

        this._applyServerFilterGrid();
        this._applyServerFilterTree();
    },

    filterTasks: function (filter) {
        for (var i = 0; i < this._jobs.length; i++) {
            this._jobs[i].set_taskFilter(filter);
        }
    },

    rebindServersGrid: function () {
        if (this._serverGridClientID) {
            var serversGrid = $find(this._serverGridClientID);

            if (serversGrid) {
                var masterTable = serversGrid.get_masterTableView();

                this._saveExpandedServers();
                this._collapseAllServers();

                masterTable.set_dataSource(this.get_serversList());
                masterTable.dataBind();
                masterTable.set_virtualItemCount(this.get_serversList().length);

                serversGrid.repaint();

                this._restoreExpandedServers();
            }
        }
    },

    sortJobs: function (gridId, fieldName, sortOrder) {
        var recordId = this._jobs.findSingle(function (toTest) { return toTest.get_gridControlId() == gridId; });

        if (recordId == -1) {
            return;
        }

        this._jobs[recordId].sort(fieldName, sortOrder);
    },

    //private
    _applyServerFilterGrid: function () {
        return;

        var masterTable = $find(this._serverGridClientID).get_masterTableView();

        //we use row adjustment because it turned out that each child grid is considered to be an "item" too
        //so in order to call showItem() methods and so on to appropriate rows - we have to add all nested rows to the index too
        var rowAdjustment = 0;

        var dataItems = masterTable.get_dataItems();

        for (i = 0; i < dataItems.length; i++) {
            if (dataItems[i].getDataKeyValue("UserServerId") == this._serverFilter || this._serverFilter == "all_servers") {
                masterTable.showItem(i + rowAdjustment);
            }
            else {
                masterTable.hideItem(i + rowAdjustment);
                if (dataItems[i].get_nestedViews() != null) {
                    masterTable.collapseItem(i + rowAdjustment);
                }
            }
            if (dataItems[i].get_nestedViews() != null) {
                rowAdjustment++;
            }
        }
    },

    _applyServerFilterTree: function () {
        for (var i = 0; i < this.get_serversList().length; i++) {
            if (this.get_serversList()[i].ServerId == this._serverFilter || this._serverFilter == "all_servers") {
                this._showHideServerNode(this.get_serversList()[i].ServerId, true);
            }
            else {
                this._showHideServerNode(this.get_serversList()[i].ServerId, false);
            }
        }
    },

    _addServerNode: function (name, serverId) {
        treeView = $find(this.get_serverTreeClientID());

        if (treeView) {
            treeView.trackChanges();

            //Instantiate a new client node
            var node = new Telerik.Web.UI.RadTreeNode();
            //Set its text
            node.set_text(name);
            node.set_value(serverId);

            node.set_expandMode(Telerik.Web.UI.TreeNodeExpandMode.ServerSideCallBack);

            treeView.get_nodes().add(node);

            //node.expand();

            treeView.commitChanges();
        }
    },

    _collapseAllServers: function () {
        var masterTable = $find(this._serverGridClientID).get_masterTableView();
        var dataItems = masterTable.get_dataItems();

        for (i = 0; i < dataItems.length; i++) {
            dataItems[i].set_expanded(false);
        }
    },

    _getServerObject: function (serverId) {
        var recordId = this.get_serversList().findSingle(function (toTest) { return toTest.ServerId == serverId; });

        if (recordId != -1) {
            return this.get_serversList()[recordId];
        }

        return null;
    },

    _initServers: function (servers) {
        for (var i = 0; i < servers.length; i++) {
            this.addServer(servers[i]);
        }
    },

    _removeServerNode: function (serverId) {
        treeView = $find(this.get_serverTreeClientID());

        if (treeView) {
            treeView.trackChanges();

            var allNodes = treeView.get_allNodes();
            var nodeToDelete = null;
            for (var i = 0; i < allNodes.length; i++) {
                if (allNodes[i].get_value() == serverId) {
                    nodeToDelete = allNodes[i];
                }
            }
            treeView.get_nodes().remove(nodeToDelete);
            treeView.commitChanges();
        }
    },

    _restoreExpandedServers: function () {
        var masterTable = $find(this._serverGridClientID).get_masterTableView();
        var dataItems = masterTable.get_dataItems();

        for (i = 0; i < dataItems.length; i++) {
            var server = this._getServerObject(dataItems[i].getDataKeyValue("ServerId"));
            if (server) {
                dataItems[i].set_expanded(server.webclient_expanded);
            }
        }
    },

    _saveExpandedServers: function () {
        var masterTable = $find(this._serverGridClientID).get_masterTableView();
        var dataItems = masterTable.get_dataItems();

        for (i = 0; i < dataItems.length; i++) {
            var server = this._getServerObject(dataItems[i].getDataKeyValue("ServerId"));
            if (server) {
                server.webclient_expanded = dataItems[i].get_expanded();
            }
        }
    },

    _showHideServerNode: function (value, visible) {
        treeView = $find(this.get_serverTreeClientID());

        if (treeView) {
            var node = treeView.findNodeByValue(value);
            if (node) {
                treeView.trackChanges();
                node.set_visible(visible);
                treeView.commitChanges();
                return true;
            }
        }

        return false;
    }
}
VisualCron.VisualCronData.registerClass('VisualCron.VisualCronData', Sys.Component);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager
// that this is the end of the script.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();